﻿Imports System.Collections.Specialized

Public Class frmTabPositionalAccuracy

    Dim EditNode As EditXML = New EditXML()
    Public Shared sHPosAcc As StringCollection = New StringCollection()
    Public Shared sVPosAcc As StringCollection = New StringCollection()
    Public Shared sPdelim As String = "£"

    Public Sub editXMLFile()
        EditNode.Edit("/metadata/dataqual/posacc/horizpa/horizpar", txtHorAccRpt.Text.Trim())
        EditNode.Edit("/metadata/dataqual/posacc/vertacc/vertaccr", txtVertAccRpt.Text.Trim())

        EditNode.Remove("/metadata/dataqual/posacc/horizpa", "qhorizpa")
        EditNode.Remove("/metadata/dataqual/posacc/vertacc", "qvertpa")

        For i As Integer = sHPosAcc.Count To 1 Step -1
            Dim values() As String = sHPosAcc(i - 1).Split(sPdelim)
            EditNode.Add("/metadata/dataqual/posacc/horizpa", "/horizpar", "qhorizpa", "<horizpav>" & values(0) & "</horizpav><horizpae>" & values(1) & "</horizpae>")
        Next
        For i As Integer = sVPosAcc.Count To 1 Step -1
            Dim values() As String = sVPosAcc(i - 1).Split(sPdelim)
            EditNode.Add("/metadata/dataqual/posacc/vertacc", "/vertaccr", "qvertpa", "<vertaccv>" & values(0) & "</vertaccv><vertacce>" & values(1) & "</vertacce>")
        Next
    End Sub

    Private Sub btnAddHPosAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddHPosAcc.Click
        Dim sNewPosAcc As String = sPdelim 'create blank attribute accuracy assessment pair
        Dim frmNewPosAcc As New frmPosAcc(sNewPosAcc, "H")
        frmNewPosAcc.ShowDialog()
        If frmNewPosAcc.getUpdated() Then 'add the new assessment
            sNewPosAcc = frmNewPosAcc.getPosAcc()
            sHPosAcc.Add(sNewPosAcc)
            sNewPosAcc = Replace(sNewPosAcc, sPdelim, " m:  ")
            lboxHPosAcc.Items.Add(sNewPosAcc)
            editXMLFile()
        End If
    End Sub

    Private Sub btnEditHPosAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditHPosAcc.Click
        Dim iCount As Integer = lboxHPosAcc.Items.Count
        Dim iSelInd As Integer = lboxHPosAcc.SelectedIndex 'get selected assessment to edit
        If (iCount = 1) Then iSelInd = 0
        If (iSelInd = -1) Then
            MessageBox.Show("No Positional Assessment selected for editing!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim sNewPosAcc As String = sHPosAcc(iSelInd) 'retrieve selected positional accuracy assessment pair
        Dim frmEditPosAcc As New frmPosAcc(sNewPosAcc, "H")
        frmEditPosAcc.ShowDialog()
        If frmEditPosAcc.getUpdated() Then 'update the assessment
            sNewPosAcc = frmEditPosAcc.getPosAcc()
            sHPosAcc.RemoveAt(iSelInd) 'remove old version
            sHPosAcc.Insert(iSelInd, sNewPosAcc) 'put new version into collection at same point
            sNewPosAcc = Replace(sNewPosAcc, sPdelim, " m:  ")
            lboxHPosAcc.Items.RemoveAt(iSelInd) 'update list box -- remove old version
            lboxHPosAcc.Items.Insert(iSelInd, sNewPosAcc) 'update list box -- insert new version
            editXMLFile()
        End If
        btnEditHPosAcc.Enabled = False
        btnDelHPosAcc.Enabled = False
    End Sub

    Private Sub btnDelHPosAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelHPosAcc.Click
        Dim iSelInd As Integer = lboxHPosAcc.SelectedIndex 'get selected assessment to delete
        If (iSelInd = -1) Then
            MessageBox.Show("No Positional Assessment selected for deletion!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        sHPosAcc.RemoveAt(iSelInd) 'update string collection
        lboxHPosAcc.Items.RemoveAt(iSelInd) 'update list box
        editXMLFile()
        btnEditHPosAcc.Enabled = False
        btnDelHPosAcc.Enabled = False
    End Sub

    Private Sub btnAddVPosAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddVPosAcc.Click
        Dim sNewPosAcc As String = sPdelim 'create blank attribute accuracy assessment pair
        Dim frmNewPosAcc As New frmPosAcc(sNewPosAcc, "V")
        frmNewPosAcc.ShowDialog()
        If frmNewPosAcc.getUpdated() Then 'add the new assessment
            sNewPosAcc = frmNewPosAcc.getPosAcc()
            sVPosAcc.Add(sNewPosAcc)
            sNewPosAcc = Replace(sNewPosAcc, sPdelim, " m:  ")
            lboxVPosAcc.Items.Add(sNewPosAcc)
            editXMLFile()
        End If
    End Sub

    Private Sub btnEditVPosAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditVPosAcc.Click
        Dim iCount As Integer = lboxVPosAcc.Items.Count
        Dim iSelInd As Integer = lboxVPosAcc.SelectedIndex 'get selected assessment to edit
        If (iCount = 1) Then iSelInd = 0 'default to editing the solitary value
        If (iSelInd = -1) Then
            MessageBox.Show("No Positional Assessment selected for editing!", "Assessment selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim sNewPosAcc As String = sVPosAcc(iSelInd) 'retrieve selected positional accuracy assessment pair
        Dim frmEditPosAcc As New frmPosAcc(sNewPosAcc, "V")
        frmEditPosAcc.ShowDialog()
        If frmEditPosAcc.getUpdated() Then 'update the assessment
            sNewPosAcc = frmEditPosAcc.getPosAcc()
            sVPosAcc.RemoveAt(iSelInd) 'remove old version
            sVPosAcc.Insert(iSelInd, sNewPosAcc) 'put new version into collection at same point
            sNewPosAcc = Replace(sNewPosAcc, sPdelim, " m:  ")
            lboxVPosAcc.Items.RemoveAt(iSelInd) 'update list box -- remove old version
            lboxVPosAcc.Items.Insert(iSelInd, sNewPosAcc) 'update list box -- insert new version
            editXMLFile()
        End If
        btnEditVPosAcc.Enabled = False
        btnDelVPosAcc.Enabled = False
    End Sub

    Private Sub btnDelVPosAcc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelVPosAcc.Click
        Dim iSelInd As Integer = lboxVPosAcc.SelectedIndex 'get selected assessment to delete
        If (iSelInd = -1) Then
            MessageBox.Show("No Positional Assessment selected for deletion!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        sVPosAcc.RemoveAt(iSelInd) 'update string collection
        lboxVPosAcc.Items.RemoveAt(iSelInd) 'update list box
        editXMLFile()
        btnEditVPosAcc.Enabled = False
        btnDelVPosAcc.Enabled = False
    End Sub

    Private Sub txtHorAccRpt_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHorAccRpt.Leave
        editXMLFile()
    End Sub

    Private Sub txtVertAccRpt_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVertAccRpt.Leave
        editXMLFile()
    End Sub

    Private Sub lboxHPosAcc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxHPosAcc.SelectedIndexChanged
        If lboxHPosAcc.SelectedItems.Count > 0 Then btnEditHPosAcc.Enabled = True : btnDelHPosAcc.Enabled = True Else btnEditHPosAcc.Enabled = False : btnDelHPosAcc.Enabled = False
    End Sub

    Private Sub lboxVPosAcc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxVPosAcc.SelectedIndexChanged
        If lboxVPosAcc.SelectedItems.Count > 0 Then btnEditVPosAcc.Enabled = True : btnDelVPosAcc.Enabled = True Else btnEditVPosAcc.Enabled = False : btnDelVPosAcc.Enabled = False
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("PositionalAccuracy")
        helpForm.ShowDialog()
    End Sub
End Class